
function timeObj(i) {
	
	var time = new Object();
	// Add minutes and seconds
	time.m = Math.floor(i/60);
	time.s = i%60;
	
	// Add leading
	time.m_leading = (time.m < 10) ? "0"+time.m : time.m;
	time.s_leading = (time.s < 10) ? "0"+time.s : time.s;
	
	return time;
	
}

// Country
function test_is_from_high_bw(suffix) {

	var high_bw_ar = ["us","ca","au","jp","kr","ru","us","it","dk","no","se","ae","ad","bg","ct","en","cz","ee","fi","fo","gr","he","hu","ie","il","is","lt","lu","lv","md","mt","pl","ro","rt","si","at","be","nl","de","fr","es","pt","sk","sm","ua","wa","nz"];
	
	return ($.inArray(suffix.toLowerCase(), high_bw_ar) >= 0);
	
}

// Color functions
function get_semi_desatuated_color_rgb(hex) {
	
	if (!hex) hex = "333333";
	
	// Split into RGB
	if (hex.length > 6) hex = hex.substr(-6);

	var r = parseInt(hex.substr(0,2), 16);
	var g = parseInt(hex.substr(2,2), 16);
	var b = parseInt(hex.substr(4,2), 16);

	// ... and make into HSV
	var hsv = rgbToHsv(r,g,b);

	// Set value and turn back into RGB
	var new_value = (hsv[2] > 0.9) ? 0.5 : 0.3;
	var rgb = hsvToRgb(hsv[0],hsv[1],new_value);

	// Format
	for (var i in rgb) {
		rgb[i] = Math.floor(rgb[i]);
	}
	rgb = rgb.join(",");

	return rgb;

}

function get_inverted_hue_rgb(hex) {

	// Split into RGB
	if (hex.length > 6) hex = hex.substr(-6);

	var r = parseInt(hex.substr(0,2), 16);
	var g = parseInt(hex.substr(2,2), 16);
	var b = parseInt(hex.substr(4,2), 16);

	// ... and make into HSV
	var hsv = rgbToHsv(r,g,b);

	// Set value and turn back into RGB
	var hue = (hsv[0] > 0.5) ? hsv[0]-0.5 : hsv[0]+0.5;
	var rgb = hsvToRgb(hue,hsv[1],0.5);

	// Format
	for (var i in rgb) {
		rgb[i] = Math.floor(rgb[i]);
	}
	rgb = rgb.join(",");

	return rgb;
}

function get_color_number_scope(val) {
	if (val < 4 || val == "-") return "#999";
	if (val < 7) return "#CCC";
	return "#FFF";
}

function get_rgb_from_hex(hex) {
	
	hex = (!hex) ? "000000" : hex;
	
	// Split into RGB
	if (hex.length > 6) hex = hex.substr(-6);

	var r = parseInt(hex.substr(0,2), 16);
	var g = parseInt(hex.substr(2,2), 16);
	var b = parseInt(hex.substr(4,2), 16);

	// ... and make into HSV
	var hsv = rgbToHsv(r,g,b);

	return hsv;

}

function get_team_color_from_team_id(id) {
	var team_side = get_team_side_from_team_id(id);
	if (team_side === 0) return "rgb("+team_colors["home"]["rgb"]+")";
	if (team_side === 1) return "rgb("+team_colors["away"]["rgb"]+")";
	return "rgb(34,34,34)";
}

// Color functions end

// Misc. Functions

function get_player_line(position) {
	if (!position) return false;
	if (position == "gk") { return "gk"; }
	else if (position.indexOf("dm")>=0) { return "dm"; }
	else if (position.indexOf("om")>=0) { return "om"; }
	else if (position.indexOf("d")>=0) { return "d"; }
	else if (position.indexOf("m")>=0) { return "m"; }
	else if (position.indexOf("f")>=0) { return "f"; }
	return false;
}
function get_player_side(position) {
	if (position == "gk") { return "c"; }
	else if (position.indexOf("cl")>=0) { return "cl"; }
	else if (position.indexOf("cr")>=0) { return "cr"; }
	else if (position.indexOf("l")>=0) { return "l"; }
	else if (position.indexOf("c")>=0) { return "c"; }
	else if (position.indexOf("r")>=0) { return "r"; }
	return false;
}


// Sort functions
function sort_standing(a, b) {
	var a_real_points = a["points"]+a["bonus"];
	var b_real_points = b["points"]+b["bonus"];

	// Main sort; points
	if (a_real_points == b_real_points) {

		// Tie breaker 1; goal dif.
		var a_goal_dif = a["goals"]-a["goals_against"];
		var b_goal_dif = b["goals"]-b["goals_against"];

		if (a_goal_dif == b_goal_dif) {
			// Tie breaker 2; goals for.
			return b["goals"]-a["goals"];
		}

		return b_goal_dif-a_goal_dif;

	}
	return b_real_points-a_real_points;
}
function sort_position(a,b) {
	var value_ar = {"gk":1,"d":2,"dm":3,"m":4,"om":5,"f":6};

	var a_value = value_ar[get_player_line(a["start_position"])] || 7;
	var b_value = value_ar[get_player_line(b["start_position"])] || 7;

	return b_value - a_value;
}
// Sort functions end


// Search functions
function get_player_from_id(id) {
	if (lineup["home"][id]) {
		return lineup["home"][id];
	} else if (lineup["away"][id]) {
		return lineup["away"][id];
	}
	return false;
}

function get_team_side_from_team_id(id) {
	if (parseInt(id) == parseInt(away_team["id"])) {
		return 1;
	} else if (parseInt(id) == parseInt(home_team["id"])) {
		return 0;
	}
	return false;
}

function get_team_side_from_player_id(id) {
	if (lineup["away"][parseInt(id)]) {
		return 1;
	} else if (lineup["home"][parseInt(id)]) {
		return 0;
	}
	return false;
}
// Search functions end




function get_match_type(type) {
	if (type == "clc" || type == "uec" || type == "clg" || type == "ueg") { return "international"; }
	else if (type.charAt(0) == "p") { return "cup"; }
	else if (type == "l" || type == "lq") { return "league"; }
	else if (type == "f" || type == "fl") { return "friendly"; }
	return "-";
}

function get_match_type_lang(type) {
	if (type == "clc" || type == "uec" || type == "clg" || type == "ueg") { return match_type_language_ar["international_cup"]; }
	else if (type.charAt(0) == "p") { return match_type_language_ar["cup"]; }
	else if (type == "l") { return match_type_language_ar["league"]; }
	else if (type == "f") { return match_type_language_ar["friendly"]; }
	else if (type == "fl") { return match_type_language_ar["friendly_league"]; }
	return "-";
}





// ^^^ DONE ^^^





function get_weather_url(str) {
	if (str.indexOf("sunny") >= 0) {
		return "http://static.trophymanager.com/pics/matchviewer/weather_sun.png";
	} else if (str.indexOf("cloudy") >= 0) {
		return "http://static.trophymanager.com/pics/matchviewer/weather_cloud.png";
	} else if (str.indexOf("rainy") >= 0) {
		return "http://static.trophymanager.com/pics/matchviewer/weather_rain.png";
	} else if (str.indexOf("snowy") >= 0) {
		return "http://static.trophymanager.com/pics/matchviewer/weather_snow.png";
	}
	return "http://static.trophymanager.com/pics/matchviewer/weather_unknown.png";
}
function get_player_link_text(id) {
	var player = get_player_from_id(id);
	if (player) return "<a class='none' player_id='"+id+"' href='/players/"+id+"'>"+player["name"]+"</a>";
	return "<span>Retired</span>";
	//return false;
}


function test_colors_are_close(hex_home, hex_away) {

	var home_color_hsv = get_rgb_from_hex(hex_home);
	var away_color_hsv = get_rgb_from_hex(hex_away);
	var close = true;

	var home_hue = home_color_hsv[0];
	var away_hue = away_color_hsv[0];

	if (away_hue > 0.75 && home_hue < 0.25) {
		home_hue = home_hue+1;
	} else if (home_hue > 0.75 && away_hue < 0.25) {
		away_hue = away_hue+1;
	}

	var hue_delta = Math.abs(home_hue-away_hue);
	var satuation_delta = Math.abs(home_color_hsv[1]-away_color_hsv[1]);
	var value_delta = Math.abs(home_color_hsv[2]-away_color_hsv[2]);

	//if (hue_delta > 0.20 || satuation_delta > 0.3 || value_delta > 0.5) {
	if (hue_delta > 0.20 || satuation_delta > 0.3) {
		close = false;
	}
	/*
	onsole.log("HUE: "+home_hue+" / "+away_hue+" - "+hue_delta+" >> "+(hue_delta > 0.15));
	onsole.log("SAT: "+home_color_hsv[1]+" / "+away_color_hsv[1]+" - "+satuation_delta+" >> "+(satuation_delta > 0.2));
	onsole.log("VAL: "+home_color_hsv[2]+" / "+away_color_hsv[2]+" - "+value_delta+" >> "+(value_delta > 0.5));
	onsole.log("Resault: "+close);
	*/
	return close;
}